#!/usr/bin/env python
# coding=utf-8
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.escape
import tornado.auth
import tornado.web
import os.path
import model
import hashlib

from tornado.options import define, options

define("port", default=80, help="run on the given port", type=int)

class App(tornado.web.Application):
    '''应用配置'''
    def __init__(self):
        #urls
        handlers = (
            (r"/", Home),
            (r"/sign/", Sign),
        )
        #settings
        settings = dict(
            cookie_secret="LoLi101Il0ve_E=cm^2RedSpeed*3",
            template_path=os.path.join(os.path.dirname(__file__), "templates"),
            static_path=os.path.join(os.path.dirname(__file__), "static"),
            xsrf_cookies=True,
        )
        tornado.web.Application.__init__(self, handlers, **settings)

class Base(tornado.web.RequestHandler):
    def get_current_user(self):
        user = self.get_secure_cookie("user")
        if not user: return None
        return tornado.escape.json_decode(user)

class Home(Base):
    def get(self):
        self.render("home.html")
#User
class Sign(Base):
    def get(self):
        self.render("sign.html")

class SignUp(Base):
    def post(self):
        user = {
            "username" : self.get_argument("username"),
            "passworld" : self.get_argument("password")
        }
        user['password'] = hashlib.sha1(user['password']).hexdigest()

class SignIn(Base):
    def post(self):
        pass

class UserSettings(Base):
    def get(self):
        pass
    def post(self):
        pass

class UserHome(Base):
    def get(self):
        pass

class UserFollow(Base):
    def get(self):
        pass

class UserFollowing(Base):
    def get(self):
        pass

class UserFollowed(Base):
    def get(self):
        pass

#String and item
class String(Base):
    def get(self):
        pass

class NewString(Base):
    def post(self):
        pass

class NewTag(Base):
    def post(self):
        pass

class ViewTag(Base):
    def get(self):
        pass

class NewItem(Base):
    def post(self):
        pass

class LikeItem(Base):
    def get(self):
        pass

#run
def main():
    model.setup_all()
    tornado.options.parse_command_line()
    app = App()
    app.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    main()
